﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Collections.Specialized;
using CoachingOrganizer.Logic.Models.Tools.MoneyCalculation;

namespace CoachingOrganizer.Android.Adapters.Tools.MoneyCalculation
{
    public class OccasionalIncomesAdapter : BaseAdapter
    {
        private readonly Activity _context;
        private readonly List<OccasionalMoneyFlowItemModel> _items = new List<OccasionalMoneyFlowItemModel>();
        private readonly MoneyCalculationModel _model;

        public OccasionalIncomesAdapter(Activity context, MoneyCalculationModel model)
            : base()
        {
            _context = context;
            _model = model;
            model.Incomes.OccasionalMoneyFlowItemsCollectionChanged += Incomes_OccasionalMoneyFlowItemsCollectionChanged;            
        }

        public override Java.Lang.Object GetItem(int position)
        {
            return position;
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override int Count
        {
            get { return _items.Count; }
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = _context.LayoutInflater.Inflate(Resource.Layout.OccasionalIncomesItemLayout, parent, false);
            HookModelToView(view, _items[position]);
            return view;
        }

        private void HookModelToView(View view, OccasionalMoneyFlowItemModel occasionalMoneyFlowItemModel)
        {
            EditText nameEditText = view.FindViewById<EditText>(Resource.Id.nameEditText);
            EditText amountEditText = view.FindViewById<EditText>(Resource.Id.amountEditText);
            nameEditText.Text = occasionalMoneyFlowItemModel.Name;
            amountEditText.Text = occasionalMoneyFlowItemModel.Amount.ToString();
            nameEditText.TextChanged += (sender, e) =>
            {
                occasionalMoneyFlowItemModel.Name = nameEditText.Text;
                occasionalMoneyFlowItemModel.Save();
            };
            amountEditText.TextChanged += (sender, e) =>
            {
                decimal amount;
                if (decimal.TryParse(amountEditText.Text, out amount))
                {
                    occasionalMoneyFlowItemModel.Amount = amount;
                    occasionalMoneyFlowItemModel.Save();
                }
            };
            Button removeOccasionalIncomesItemButton = view.FindViewById<Button>(Resource.Id.removeOccasionalIncomesItemButton);
            removeOccasionalIncomesItemButton.Click += (sender, e) => occasionalMoneyFlowItemModel.Remove();
            DatePicker occasionalIncomesItemDatePicker = view.FindViewById<DatePicker>(Resource.Id.occasionalIncomesItemDatePicker);
            occasionalIncomesItemDatePicker.UpdateDate(occasionalMoneyFlowItemModel.Time.Year, occasionalMoneyFlowItemModel.Time.Month, occasionalMoneyFlowItemModel.Time.Day);
            occasionalIncomesItemDatePicker.Click += (sender, e) =>
            {
                occasionalMoneyFlowItemModel.Time = new DateTime(occasionalIncomesItemDatePicker.Year, occasionalIncomesItemDatePicker.Month, occasionalIncomesItemDatePicker.DayOfMonth);
                occasionalMoneyFlowItemModel.Save();
            };
            TimePicker occasionalIncomesItemTimePicker = view.FindViewById<TimePicker>(Resource.Id.occasionalIncomesItemTimePicker);
            occasionalIncomesItemTimePicker.CurrentHour = (Java.Lang.Integer)occasionalMoneyFlowItemModel.Time.Hour;
            occasionalIncomesItemTimePicker.CurrentMinute = (Java.Lang.Integer)occasionalMoneyFlowItemModel.Time.Minute;
            occasionalIncomesItemTimePicker.TimeChanged += (sender, e) =>
            {
                occasionalMoneyFlowItemModel.Time = occasionalMoneyFlowItemModel.Time.Date + new TimeSpan((int)occasionalIncomesItemTimePicker.CurrentHour, (int)occasionalIncomesItemTimePicker.CurrentMinute, 0);
                occasionalMoneyFlowItemModel.Save();
            };
        }

        private void Incomes_OccasionalMoneyFlowItemsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    _items.AddRange(e.NewItems.OfType<OccasionalMoneyFlowItemModel>());
                    break;
                case NotifyCollectionChangedAction.Remove:
                    e.OldItems.OfType<OccasionalMoneyFlowItemModel>().ToList().ForEach(i =>
                        _items.Remove(i));
                    break;
                default:
                    break;
            }
            NotifyDataSetChanged();
        }
    }
}